# app/routes/file.py
from flask import Blueprint, jsonify, request
from app.utils.security import token_required
from app.services.friend_service import FriendService
from app.models import File
from app.utils.response_utils import (
    success_response,
    error_response,
    ResponseCode
)

# 创建蓝图对象
friend_bp = Blueprint('friends', __name__)


@friend_bp.route('/getFriendGroup', methods=['POST'])
@token_required
def get_friend_group(currentUserId):
    result = FriendService.get_friend_group(user_id=currentUserId)
    print(result)
    if isinstance(result, tuple):  # 如果是错误响应
        return error_response(
            message=result[0],
            http_code=result[1]
        )

    return success_response(
        data=result,
        message='操作成功'
    )

@friend_bp.route('/addFriendGroup', methods=['POST'])
@token_required
def add_friend_group(currentUserId):
    data = request.get_json()
    group_name = data.get('group_name')

    if not group_name:
        return error_response(
            code=ResponseCode.PARAM_ERROR,
            message='参数错误',
        )

    result = FriendService.add_friend_group(user_id=currentUserId,group_name=group_name)

    if isinstance(result, tuple):  # 如果是错误响应
        return error_response(
            message=result[0],
            http_code=result[1]
        )

    return success_response(
        message='成功添加分组'
    )

@friend_bp.route('/addFriend', methods=['POST'])
@token_required
def add_friend(currentUserId):
    data = request.get_json()
    friend_id = data.get('friend_id')

    if not friend_id:
        return error_response(
            code=ResponseCode.PARAM_ERROR,
            message='参数错误',
        )

    result = FriendService.add_friend(user_id=currentUserId,friend_id=friend_id)

    if isinstance(result, tuple):  # 如果是错误响应
        return error_response(
            message=result[0],
            http_code=result[1]
        )

    return success_response(
        message='已发送好友申请'
    )

@friend_bp.route('/deleteFriend', methods=['POST'])
@token_required
def delete_friend(currentUserId):
    data = request.get_json()
    friend_id = data.get('friend_id')

    if not friend_id:
        return error_response(
            code=ResponseCode.PARAM_ERROR,
            message='参数错误',
        )

    result = FriendService.delete_friend(user_id=currentUserId,friend_id=friend_id)

    if isinstance(result, tuple):  # 如果是错误响应
        return error_response(
            message=result[0],
            http_code=result[1]
        )

    return success_response(
        message='已删除好友'
    )

@friend_bp.route('/changeFriendStatus', methods=['POST'])
@token_required
def change_friend_status(currentUserId):
    data = request.get_json()
    friend_id = data.get('friend_id')
    status = data.get('status')

    if not friend_id or not status:
        return error_response(
            code=ResponseCode.PARAM_ERROR,
            message='参数错误',
        )

    result = FriendService.change_friend_status(user_id=currentUserId,friend_id=friend_id,status=status)

    if isinstance(result, tuple):  # 如果是错误响应
        return error_response(
            message=result[0],
            code=result[1]
        )

    return success_response(
        message='操作成功'
    )

@friend_bp.route('/moveToGroup', methods=['POST'])
@token_required
def move_to_group(currentUserId):
    data = request.get_json()
    friend_id = data.get('friend_id')
    group_id = data.get('group_id')

    if not friend_id or not group_id:
        return error_response(
            code=ResponseCode.PARAM_ERROR,
            message='参数错误',
        )

    result = FriendService.movo_to_group(user_id=currentUserId,friend_id=friend_id,group_id=group_id)

    if isinstance(result, tuple):  # 如果是错误响应
        return error_response(
            message=result[0],
            code=result[1]
        )

    return success_response(
        message='操作成功'
    )
